/**  
* @Title: MessageController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年7月26日  
*/
package com.fortune.rainshine.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.system.service.MessageService;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.service.MessageModel;
import com.fortune.rainshinecommon.model.system.User;

/**  
* @ClassName: MessageController  
* @Description: TODO(消息管理)  
* @author 张鑫
* @phone 17561783855
* @date 2019年7月26日  
*    
*/
@Controller
@RequestMapping("/system/message")	
public class MessageController extends BaseController{
	private final static Logger logger = LoggerFactory.getLogger(MessageController.class);
	
	@Autowired
    private MessageService service;	
	
	@RequiresPermissions("message:view")
	@RequestMapping()
	public String message(Model model) {
		User user =this.getLoginUser();
		Map<String,Integer> map = service.selectCount(user.getUserId());
		model.addAttribute("total", map.get("num12")+map.get("num3"));
		model.addAttribute("num12", map.get("num12"));
		model.addAttribute("num3", map.get("num3"));
		
		return "message/index.html";
	}
	
	@RequiresPermissions("message:view")
	@RequestMapping("/detail")
	public String detail(Model model,Integer id) {
		MessageModel message = service.findByid(id);
		model.addAttribute("message", message);
		service.updateState(id);
		return "message/detail.html";
	}
	
	/**
     * @Title: list
     * @Description: 预约列表
     * @author zhangxin
     * @date 2019年3月26日
     */
    @RequiresPermissions("message:view")
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MsgModel list(Integer page, Integer limit, String searchKey, String searchValue,Integer type) {
    	 if (page == null) {
             page = 0;
             limit = 0;
         }
         if (StringUtil.isBlank(searchValue)) {
             searchKey = null;
         }
         User user =this.getLoginUser();
         
         ParmsModel parm = new ParmsModel(page,limit,searchKey,searchValue);
         parm.setType(type);
         parm.setUserId(user.getUserId());
         MsgModel ms = service.list(parm);
         return ms;
    }
    
    @RequiresPermissions("message:view")
    @ResponseBody
    @RequestMapping("/changeStates")
    public MsgModel changeStates(@RequestBody String ids) {
    	 try {
    		 return service.changeStates(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return new MsgModel(ResultCode.FAILURE,"系统错误");
		}
    }
    
    @RequiresPermissions("message:view")
    @ResponseBody
    @RequestMapping("/del")
    public MsgModel del(@RequestBody String id) {
    	 try {
    		 return service.del(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new MsgModel(ResultCode.FAILURE,"系统错误");
		}
    }

}
